/**
 * Author: Ravi Tamada
 * URL: www.androidhive.info
 * twitter: http://twitter.com/ravitamada
 * */
package com.vg.library;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.Parser;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class UserFunctions {

	private JSONParser jsonParser;
	Context context;

	private static String loginURL = "http://192.168.0.148/android_login_api/";
	private static String registerURL = "http://192.168.0.148/android_login_api/";
	private static String loginarrayURL = "http://192.168.0.138/json/users.php";
	private static String loginarrayURL1 = "http://192.168.0.138/json/login.php";

	private static String login_tag = "login";
	private static String register_tag = "register";

	// constructor
	public UserFunctions() {
		jsonParser = new JSONParser();
	}
	
	public JSONArray loginUser1(String email) {
		// Building Parameters
		
	//	String md5=getMd5Hash(password);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
	//	params.add(new BasicNameValuePair("tag", login_tag));
		params.add(new BasicNameValuePair("use_login", email));
		Log.i("1",""+ params.toString());
	//	params.add(new BasicNameValuePair("use_password", md5));
		JSONArray json = jsonParser.getJSONArrayFromUrl("http://192.168.0.138/json/user_info.php", params);

		// return json
		Log.e("JSON LoginUser111", json.toString());
		return json;

	}

	

	public JSONArray loginArrayUser(String email,String password) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		// params.add(new BasicNameValuePair("tag", login_tag));
		params.add(new BasicNameValuePair("use_login", email));
		params.add(new BasicNameValuePair("use_password", password));
		JSONArray json = jsonParser.getJSONArrayFromUrl(loginarrayURL, params);
		// return json
		Log.e("JSON LoginUserAraayyyyyy", json.toString());
		return json;
	}
	
	public JSONArray loginArrayUser1(String email, String password) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		// params.add(new BasicNameValuePair("tag", login_tag));
		params.add(new BasicNameValuePair("use_login",email));
		params.add(new BasicNameValuePair("use_password",password));
		JSONArray json = jsonParser.getJSONArrayFromUrl(loginarrayURL1, params);
		// return json
		Log.e("JSON array", json.toString());
		return json;

	}
	
//	public String md5(String s) {
//	    try {
//	        // Create MD5 Hash
//	        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
//	        digest.update(s.getBytes());
//	        byte messageDigest[] = digest.digest();
//	        
//	        // Create Hex String
//	        StringBuffer hexString = new StringBuffer();
//	        for (int i=0; i<messageDigest.length; i++)
//	            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
//	        return hexString.toString();
//	        
//	    } catch (NoSuchAlgorithmException e) {
//	        e.printStackTrace();
//	    }
//	    return "";
//	}
	
	public static String getMd5Hash(String input) {
		 try {
		 MessageDigest md = MessageDigest.getInstance("MD5");
		 byte[] messageDigest = md.digest(input.getBytes());
		 BigInteger number = new BigInteger(1, messageDigest);
		 String md5 = number.toString(16);
		 
		 while (md5.length() < 32)
		 md5 = "0" + md5;
		 
		 return md5;
		 } catch (NoSuchAlgorithmException e) {
		 Log.e("MD5", e.getLocalizedMessage());
		 return null;
		 }
		 }


	/**
	 * function make Login Request
	 * 
	 * @param email
	 * @param password
	 * */
	public JSONObject loginUser(String email, String password) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", login_tag));
		Log.i("1-1", params.toString());
		params.add(new BasicNameValuePair("email", email));
		Log.i("1-2", params.toString());
		params.add(new BasicNameValuePair("password", password));
		Log.i("1-3", params.toString());
		JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
		
		// return json
		Log.e("JSON LoginUser", json.toString());
		return json;

	}

	/**
	 * function make Login Request
	 * 
	 * @param name
	 * @param email
	 * @param password
	 * */
	public JSONObject registerUser(String name, String email, String password) {
		// Building Parameters
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("tag", register_tag));
		params.add(new BasicNameValuePair("name", name));
		params.add(new BasicNameValuePair("email", email));
		params.add(new BasicNameValuePair("password", password));

		// getting JSON Object
		JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
		// return json
		return json;
	}

	/**
	 * Function get Login status
	 * */
	public boolean isUserLoggedIn(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		int count = db.getRowCount();
		if (count > 0) {
			// user logged in
			return true;
		}
		return false;
	}

	/**
	 * Function to logout user Reset Database
	 * */
	public boolean logoutUser(Context context) {
		DatabaseHandler db = new DatabaseHandler(context);
		db.resetTables();
		return true;
	}

}
